package com.swgk.core.dialog;

import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.util.DisplayMetrics;
import android.view.Gravity;
import android.view.Window;
import android.view.WindowManager;

import com.swgk.core.R;

public abstract class BaseBottomDialog extends Dialog {
    private Bundle savedInstanceState;
    protected Activity context;
    protected DialogCallBack dialogCallBack;
    protected PhotoCallBack photoCallBack;

    public BaseBottomDialog(Context context, DialogCallBack dialogCallBack) {
        this(context, R.style.dialog_fullscreen, dialogCallBack);
    }

    public BaseBottomDialog(Context context, int theme, DialogCallBack dialogCallBack) {
        super(context, theme);
        this.context = (Activity) context;
        this.dialogCallBack = dialogCallBack;
        init(savedInstanceState);
    }

    public BaseBottomDialog(Context context, int theme, DialogCallBack dialogCallBack,int gravity) {
        super(context, theme);
        this.context = (Activity) context;
        this.dialogCallBack = dialogCallBack;
        init(savedInstanceState,gravity);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        this.savedInstanceState = savedInstanceState;
    }

    private void init(Bundle savedInstanceState) {
        DialogViewHolder dialogViewHolder = DialogViewHolder.get(context, null, dialogLayout());
        setContentView(dialogViewHolder.getDialog());
        initView(dialogViewHolder,savedInstanceState);
        windowDeploy();
    }

    private void init(Bundle savedInstanceState,int gravity) {
        DialogViewHolder dialogViewHolder = DialogViewHolder.get(context, null, dialogLayout());
        setContentView(dialogViewHolder.getDialog());
        initView(dialogViewHolder,savedInstanceState);
        setWindowDeploy(gravity);
    }

    //设置窗口显示
    public void windowDeploy() {
        Window window = getWindow(); //得到对话框
        window.setWindowAnimations(R.style.dialogWindowAnim); //设置窗口弹出动画
        window.setBackgroundDrawableResource(R.color.transparent); //设置对话框背景为透明
        WindowManager manager = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
        DisplayMetrics dm = new DisplayMetrics();
        manager.getDefaultDisplay().getMetrics(dm);
        WindowManager.LayoutParams lp = window.getAttributes();
        lp.gravity = Gravity.BOTTOM;
        lp.width = dm.widthPixels;
        lp.height = WindowManager.LayoutParams.WRAP_CONTENT;
        window.setAttributes(lp);
    }

    //设置窗口显示
    public void setWindowDeploy(int gravity) {
        Window window = getWindow(); //得到对话框
        window.setWindowAnimations(R.style.popwindow_right); //设置窗口弹出动画
        window.setBackgroundDrawableResource(R.color.transparent); //设置对话框背景为透明
        WindowManager manager = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
        DisplayMetrics dm = new DisplayMetrics();
        manager.getDefaultDisplay().getMetrics(dm);
        WindowManager.LayoutParams lp = window.getAttributes();
        lp.gravity = gravity;
        lp.width = WindowManager.LayoutParams.WRAP_CONTENT;
        lp.height = WindowManager.LayoutParams.WRAP_CONTENT;
        window.setAttributes(lp);
    }

    @Override
    public void cancel() {
        super.cancel();

    }

    @Override
    public void show() {
        super.show();
    }

    public interface DialogCallBack {
        void onSelect(String condition);
    }

    public void setDialogCallBack(DialogCallBack dialogCallBack) {
        this.dialogCallBack = dialogCallBack;
    }

    public interface PhotoCallBack {
        void take();

        void choose();
    }

    public void setPhotoCallBack(PhotoCallBack photoCallBack) {
        this.photoCallBack = photoCallBack;
    }

    protected abstract int dialogLayout();

    protected abstract void initView(DialogViewHolder holder,Bundle savedInstanceState);
}
